package com.util;

import com.CodeMessageProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Properties;

@Component
@Data
@NoArgsConstructor
public class CodeMsg {


    /**
     * 接口统一编码
      */
    private int code;

    /**
     *  接口统一信息回调
     */
    private String msg;

    public static Properties messageprop;

    @PostConstruct
    public void getPropInit() {
        Properties prop = new Properties();
        try {
            prop.load(CodeMessageProperties.class.getClassLoader().getResourceAsStream("codemessage.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        CodeMsg.messageprop = prop;
    }

    private CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 带自定义格式化参数的错误信息
     * @param args  信息参数
     * @return 接口统一回调模型
     */
    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code,message);
    }

    /**
     * 通用异常
     */
    public static CodeMsg PWD_ERROR = new CodeMsg(400, "用户名或密码错误");
    public static CodeMsg INFO_NULL = new CodeMsg(401, "用户名或密码为空");
    public static CodeMsg CUSTOMER_INFO_NULL = new CodeMsg(402, "查询到的客户为空");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
    public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
	public static CodeMsg INSERT_INFO = new CodeMsg(500102, "插入失败");
    /**
     * 登录模块5002XX
     */

    public static final CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "密码不能为空");
    public static final CodeMsg MOBILE_EMPTY = new CodeMsg(500211, "手机号不能为空");
    public static final CodeMsg MOBILE_ERROR = new CodeMsg(500211, "手机号格式错误");
    public static final CodeMsg USER_NOT_EXITS = new CodeMsg(500211, "用户不存在");
    public static final CodeMsg PASSWORD_ERROR = new CodeMsg(500211, "密码错误");
    public static final CodeMsg SESSION_TIMOUT = new CodeMsg(500500,"登录过期请重新登录");
}